//
//  SISSHouseEnrollSecondViewModel.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/9/3.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSHouseEnrollSecondViewModel.h"

#import "SISSHouseEnrollSecondViewController.h"
#import "SWForm.h"
#import "SISSPickerView.h"
#import "SISSHouseResourceTagView.h"
#import "SISSRecommendPriceViewController.h"

@interface SISSHouseEnrollSecondViewModel ()

@property (nonatomic, strong) NSMutableArray *mutableItems;

/// controller
@property (weak, nonatomic) SISSHouseEnrollSecondViewController *controller;

/** propertyIndex */
@property (assign, nonatomic) NSInteger propertyIndex;
/** areaFirstIndex */
@property (assign, nonatomic) NSInteger areaFirstIndex;
/** areaSecondIndex */
@property (assign, nonatomic) NSInteger areaSecondIndex;
/** houseTypeIndex */
@property (assign, nonatomic) NSInteger houseTypeIndex;
/** directionIndex */
@property (assign, nonatomic) NSInteger directionIndex;
/** decorationIndex */
@property (assign, nonatomic) NSInteger decorationIndex;
/** buildAgeIndex */
@property (assign, nonatomic) NSInteger buildAgeIndex;

@end
@implementation SISSHouseEnrollSecondViewModel


- (instancetype)initWithController:(SISSHouseEnrollSecondViewController *)controller {
    if (self = [super init]) {
        self.controller = controller;
    }
    return self;
}

/**
 产权选择点击事件
 */
- (void)propertyChooseTapAction {
    
    [self.controller.view endEditing:YES];
    //    if (self.houseModel) return;
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.controller.view.bounds];
    pickerView.title = @"产权";
    pickerView.titleArray = @[@"存在共有", @"不存在共有"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.propertyIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        weakself.propertyItem.info = [NSString stringWithFormat:@"%@",firstTitle];
        //        weakself.propertyLabel.textColor = COLOR(0x334A60);
        //        weakself.propertyLabel.font = [UIFont boldSystemFontOfSize:13];
        //        weakself.propertyLabel.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.propertyIndex = firstIndex;
        [self.controller.formTableView reloadData];
        [self houseTypeChooseTapAction];
    };
}


/**
 户型选择点击事件
 */
- (void)houseTypeChooseTapAction {
    
    [self.controller.view endEditing:YES];
    //    if (self.houseModel) return;
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.controller.view.bounds];
    pickerView.title = @"户型";
    pickerView.titleArray = @[@"一房", @"二房", @"三房", @"四房", @"四房以上", @"复式", @"别墅", @"其他"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.houseTypeIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        //        weakself.houseTpyeLabel.textColor = COLOR(0x334A60);
        //        weakself.houseTpyeLabel.font = [UIFont boldSystemFontOfSize:13];
        //        weakself.houseTpyeLabel.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.houseTypeItem.info = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.houseTypeIndex = firstIndex;
        [self.controller.formTableView reloadData];
    };
}

/**
 朝向选择点击事件
 */
- (void)directionChooseTapAction {
    
    [self.controller.view endEditing:YES];
    //    if (self.houseModel) return;
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.controller.view.bounds];
    pickerView.title = @"朝向";
    pickerView.titleArray = @[@"朝东", @"朝南", @"朝西", @"朝北", @"东南", @"东北", @"西北", @"西南"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.directionIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        //        weakself.directionLabel.textColor = COLOR(0x334A60);
        //        weakself.directionLabel.font = [UIFont boldSystemFontOfSize:13];
        //        weakself.directionLabel.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.directionItem.info = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.directionIndex = firstIndex;
        [self.controller.formTableView reloadData];
        [self decorationChooseTapAction];
        
    };
}

/**
 装修程度选择点击事件
 */
- (void)decorationChooseTapAction {
    
    [self.controller.view endEditing:YES];
    //    if (self.houseModel) return;
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.controller.view.bounds];
    pickerView.title = @"装修程度";
    pickerView.titleArray = @[@"毛坯", @"普装", @"精装", @"豪装"];
    pickerView.isArea = NO;
    pickerView.firstIndex = self.decorationIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        //        weakself.decorationLabel.textColor = COLOR(0x334A60);
        //        weakself.decorationLabel.font = [UIFont boldSystemFontOfSize:13];
        //        weakself.decorationLabel.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.decorationItem.info = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.decorationIndex = firstIndex;
        [self.controller.formTableView reloadData];
        [self buldingAgeChooseTapAction];
    };
}

/**
 建筑年代选择点击事件
 */
- (void)buldingAgeChooseTapAction {
    
    [self.controller.view endEditing:YES];
    //    if (self.houseModel) return;
    
    //获取当前时间 （时间格式支持自定义）
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];//自定义时间格式
    NSString *currentDateStr = [formatter stringFromDate:[NSDate date]];
    //拆分年月成数组
    NSArray *dateArray = [currentDateStr componentsSeparatedByString:@"-"];
    NSMutableArray *yearArray;
    NSInteger currentYear = [[dateArray firstObject]integerValue];
    
    //初始化年数据源数组
    yearArray = [[NSMutableArray alloc]init];
    for (NSInteger i = currentYear ; i >= 1970 ; i--) {
        NSString *yearStr = [NSString stringWithFormat:@"%ld年",i];
        [yearArray addObject:yearStr];
    }
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.controller.view.bounds];
    pickerView.title = @"建筑年代";
    pickerView.titleArray = yearArray;
    pickerView.isArea = NO;
    pickerView.firstIndex = self.buildAgeIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        //        weakself.buldingAgeLabel.textColor = COLOR(0x334A60);
        //        weakself.buldingAgeLabel.font = [UIFont boldSystemFontOfSize:13];
        //        weakself.buldingAgeLabel.text = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.buildingYearItem.info = [NSString stringWithFormat:@"%@",firstTitle];
        weakself.buildAgeIndex = firstIndex;
        [self.controller.formTableView reloadData];
        [self houseResourceTagChooseTapAction];
    };
}

/**
 房源标签选择点击事件
 */
- (void)houseResourceTagChooseTapAction {
    
    [self.controller.view endEditing:YES];
    //    if (self.houseModel) return;
    
    SISSHouseResourceTagView *pickerView = [[SISSHouseResourceTagView alloc] initWithFrame:self.controller.view.bounds];
    
    pickerView.cacheIndexArray = self.tagIndexArray;
    __weak typeof(self) weakself = self;
    pickerView.getStringArrBlock = ^(NSMutableArray *stringArray, NSMutableArray *indexArray) {
        
        NSMutableString *mStr = [NSMutableString string];
        for (int i = 0; i < stringArray.count; i++) {
            [mStr appendFormat:@"%@ ",stringArray[i]];
        }
        //        weakself.houseResourceTagLabel.textColor = COLOR(0x334A60);
        //        weakself.houseResourceTagLabel.font = [UIFont boldSystemFontOfSize:13];
        //        weakself.houseResourceTagLabel.text = mStr.copy;
        weakself.houseTagItem.info = mStr;
        weakself.tagIndexArray = indexArray;
        [self.controller.formTableView reloadData];
    };
}

- (NSMutableArray *)dataSource {
    NSMutableArray *items = [NSMutableArray array];
    
    SWFormItem *communityItem = [SWFormItem sw_itemWithTitle:@"小区：" info:@"" itemType:SWFormItemTypeInput editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    communityItem.placeholder = @"请输入小区";
    communityItem.info = self.houseModel.neighbourhood;
    [items addObject:communityItem];
    self.communityItem = communityItem;
    
    SWFormItem *buildingItem = [SWFormItem sw_itemWithTitle:@"楼栋：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    buildingItem.info = self.houseModel.bnum;
    [items addObject:buildingItem];
    self.buildingItem = buildingItem;
    
    SWFormItem *roomItem = [SWFormItem sw_itemWithTitle:@"房号：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    roomItem.info = self.houseModel.dnum;
    [items addObject:roomItem];
    self.roomItem = roomItem;
    
    
    SWFormSectionItem *firstSectionItem = [SWFormSectionItem sw_sectionItem:items];
    //    sectionItem.headerHeight = 10;
    //    sectionItem.footerView = [self footerView];
    //    sectionItem.footerHeight = 80;
    
    NSMutableArray *secondItems = [NSMutableArray array];
    SWFormItem *priceItem = [SWFormItem sw_itemWithTitle:@"售价" info:@"" itemType:SWFormItemTypeInputSelect editable:YES required:YES keyboardType:UIKeyboardTypeDefault];
    WeakSelf
    priceItem.itemSelectCompletion = ^(SWFormItem * _Nonnull item) {
        SISSRecommendPriceViewController *vc = [[SISSRecommendPriceViewController alloc] init];
        vc.communityName = self.houseModel.neighbourhood;
        vc.recommendPriceBlock = ^(NSString *avgPrice) {
            if (avgPrice.length > 0) {
                NSString *price = [NSString stringWithFormat:@"%.4f",([avgPrice floatValue] * [self.floorAreaItem.info floatValue])*0.0001];
                
                weakSelf.priceItem.info = price;
                [self.controller.formTableView reloadData];
            }
        };
        [self.controller.navigationController pushViewController:vc animated:YES];
    };
    priceItem.btnTitle = @"推荐售价";
    priceItem.itemUnitType = SWFormItemUnitTypeMillion;
    [secondItems addObject:priceItem];
    self.priceItem = priceItem;
    
    SWFormItem *propertyItem = [SWFormItem sw_itemWithTitle:@"产权：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    propertyItem.itemSelectCompletion = ^(SWFormItem *item) {
        
        [self propertyChooseTapAction];
    };
    //    room.unit = @"";
    [secondItems addObject:propertyItem];
    self.propertyItem = propertyItem;
    
    SWFormItem *houseTypeItem = [SWFormItem sw_itemWithTitle:@"户型：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:YES keyboardType:UIKeyboardTypeDefault];
    houseTypeItem.itemSelectCompletion = ^(SWFormItem *item) {
        
        [self houseTypeChooseTapAction];
    };
    //    room.unit = @"";
    [secondItems addObject:houseTypeItem];
    self.houseTypeItem = houseTypeItem;
    
    SWFormItem *floorAreaItem = [SWFormItem sw_itemWithTitle:@"建筑面积：" info:@"" itemType:SWFormItemTypeInput editable:YES required:YES keyboardType:UIKeyboardTypeDefault];
    floorAreaItem.itemUnitType = SWFormItemUnitTypeCustom;
    floorAreaItem.unit = @"㎡";
    [secondItems addObject:floorAreaItem];
    self.floorAreaItem = floorAreaItem;
    
    SWFormItem *teachingAreaItem = [SWFormItem sw_itemWithTitle:@"套内面积：" info:@"" itemType:SWFormItemTypeInput editable:YES required:YES keyboardType:UIKeyboardTypeDefault];
    teachingAreaItem.info = @"";
    teachingAreaItem.itemUnitType = SWFormItemUnitTypeCustom;
    teachingAreaItem.unit = @"㎡";
    [secondItems addObject:teachingAreaItem];
    self.teachingAreaItem = teachingAreaItem;
    
    SWFormSectionItem *secondSectionItem = [SWFormSectionItem sw_sectionItem:secondItems];
    secondSectionItem.headerHeight = 16;
    
    NSMutableArray *thirdItems = [NSMutableArray array];
    SWFormItem *buildingYearItem = [SWFormItem sw_itemWithTitle:@"建筑年代：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:NO keyboardType:UIKeyboardTypeDefault];
    buildingYearItem.itemSelectCompletion = ^(SWFormItem *item) {
        
        [self buldingAgeChooseTapAction];
    };
    buildingYearItem.required = NO;
    [thirdItems addObject:buildingYearItem];
    self.buildingYearItem = buildingYearItem;
    
    SWFormItem *directionItem = [SWFormItem sw_itemWithTitle:@"朝向：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:NO keyboardType:UIKeyboardTypeDefault];
    directionItem.itemSelectCompletion = ^(SWFormItem *item) {
        [self directionChooseTapAction];
    };
    directionItem.required = NO;
    //    directionItem.itemType = NO;
    [thirdItems addObject:directionItem];
    self.directionItem = directionItem;
    
    SWFormItem *decorationItem = [SWFormItem sw_itemWithTitle:@"装修程度：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:NO keyboardType:UIKeyboardTypeDefault];
    decorationItem.itemSelectCompletion = ^(SWFormItem *item) {
        [self decorationChooseTapAction];
    };
    decorationItem.required = NO;
    [thirdItems addObject:decorationItem];
    self.decorationItem = decorationItem;
    
    SWFormItem *houseTagItem = [SWFormItem sw_itemWithTitle:@"房源标签：" info:@"" itemType:SWFormItemTypeSelect editable:NO required:NO keyboardType:UIKeyboardTypeDefault];
    houseTagItem.itemSelectCompletion = ^(SWFormItem *item) {
        [self houseResourceTagChooseTapAction];
    };
    houseTagItem.required = NO;
    //    directionItem.itemType = NO;
    [thirdItems addObject:houseTagItem];
    self.houseTagItem = houseTagItem;
    
    SWFormItem *contractNoItem = [SWFormItem sw_itemWithTitle:@"网签委托合同编号：" info:@"" itemType:SWFormItemTypeInput editable:YES required:NO keyboardType:UIKeyboardTypeDefault];
    [thirdItems addObject:contractNoItem];
    self.contractNoItem = contractNoItem;
    
    SWFormItem *houseInfoCodeItem = [SWFormItem sw_itemWithTitle:@"房源信息编码：" info:@"" itemType:SWFormItemTypeInput editable:YES required:NO keyboardType:UIKeyboardTypeDefault];
    [thirdItems addObject:houseInfoCodeItem];
    self.houseInfoCodeItem = houseInfoCodeItem;
    
    SWFormSectionItem *thirdSectionItem = [SWFormSectionItem sw_sectionItem:thirdItems];
    thirdSectionItem.headerHeight = 16;
    
    NSMutableArray *forthItems = [NSMutableArray array];
    SWFormItem *otherdescItem = [SWFormItem sw_itemWithTitle:@"房屋优劣势：" info:@"" itemType:SWFormItemTypeTextViewInput editable:YES required:NO keyboardType:UIKeyboardTypeDefault];
    [forthItems addObject:otherdescItem];
    self.otherdescItem = otherdescItem;
    
    SWFormSectionItem *fourthSectionItem = [SWFormSectionItem sw_sectionItem:forthItems];
    fourthSectionItem.headerHeight = 16;
    
    NSMutableArray *fifthItems = [NSMutableArray array];
    SWFormItem *sellerNameItem = [SWFormItem sw_itemWithTitle:@"卖家姓名：" info:@"" itemType:SWFormItemTypeInput editable:YES required:YES keyboardType:UIKeyboardTypeDefault];
    
    //    directionItem.itemType = NO;
    [fifthItems addObject:sellerNameItem];
    self.sellerNameItem = sellerNameItem;
    
    SWFormItem *sellerPhoneNumItem = [SWFormItem sw_itemWithTitle:@"卖家手机号码：" info:@"" itemType:SWFormItemTypeInput editable:YES required:YES keyboardType:UIKeyboardTypeDefault];
    [fifthItems addObject:sellerPhoneNumItem];
    self.sellerPhoneNumItem = sellerPhoneNumItem;
    
    SWFormSectionItem *fifthSectionItem = [SWFormSectionItem sw_sectionItem:fifthItems];
    fifthSectionItem.headerHeight = 16;
    
   
    [self.mutableItems addObject:firstSectionItem];
    [self.mutableItems addObject:secondSectionItem];
    [self.mutableItems addObject:thirdSectionItem];
    [self.mutableItems addObject:fourthSectionItem];
    [self.mutableItems addObject:fifthSectionItem];
    
    return self.mutableItems;
    
}


- (NSMutableArray *)mutableItems {
    if (!_mutableItems) {
        _mutableItems = [[NSMutableArray alloc] init];
    }
    return _mutableItems;
}

@end
